package team.archai.nexus.flow.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import team.archai.nexus.flow.config.FlowDesignerProperties;
import team.archai.nexus.flow.constant.ProcessConstant;
import team.archai.nexus.flow.service.IProcessBasicsData;
import team.archai.nexus.flow.vo.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author li shi bin
 * @desc: 默认流程基础数据
 */
@Component
@ConditionalOnProperty(
        value = {"archai-nexus.flow-designer.enabled"},
        havingValue = "true"
)
public class DefaultProcessBasicsData implements IProcessBasicsData {

    @Autowired
    private FlowDesignerProperties flowDesignerProperties;

    @Override
    public List<AssigneeTypeVO> assigneeTypes() {
        List<AssigneeTypeVO> voList = this.flowDesignerProperties.getAssigneeTypes();
        return null != voList && voList.size() != 0 ? voList : ProcessConstant.ASSIGNEE_TYPE_LIST;
    }

    @Override
    public List<ConditionCategoryVO> conditionCategories() {
        List<ConditionCategoryVO> voList = this.flowDesignerProperties.getConditionCategories();
        return null != voList && voList.size() != 0 ? voList : ProcessConstant.CONDITION_CATEGORIES;
    }

    @Override
    public List<ConditionVO> conditions(Integer conditionCategory) {
        List<ConditionVO> voList = this.flowDesignerProperties.getConditions();
        if (null == voList || voList.size() == 0) {
            voList = new ArrayList<>();
        }
        voList.addAll(ProcessConstant.CONDITIONS);
        return voList.stream().filter((r) -> r.getCategory().equals(conditionCategory)).collect(Collectors.toList());
    }

    @Override
    public List<FlowBaseVO> categories() {
        return this.flowDesignerProperties.getCategories();
    }

    @Override
    public List<FlowNodeVO> flowNodes() {
        List<FlowNodeVO> voList = this.flowDesignerProperties.getFlowNodes();
        return null != voList && voList.size() != 0 ? voList : ProcessConstant.FLOW_NODES;
    }

    @Override
    public List<FlowListenerVO> taskListeners() {
        List<FlowListenerVO> taskListenerList = new ArrayList<>(ProcessConstant.TASK_LISTENERS);
        List<FlowListenerVO> voList = this.flowDesignerProperties.getTaskListeners();
        if (null == voList || voList.size() == 0) {
            voList = new ArrayList<>();
        }
        taskListenerList.addAll(voList);
        return taskListenerList;
    }
}
